package com.zhuyp.spring.boot.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public final static String TIME = "HH:mm:ss";

    public final static String DATE = "yyyy-MM-dd";

    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    public static String getDate() {
        return df.format(new Date());
    }

    public static String getFormattedDateUtil(Date date, String Format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Format);
        return simpleDateFormat.format(date);
    }

    public static String getDateShort(long ctime) {
        if (ctime <= 0) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date(ctime)).toString();
    }

    public static String getDate(long ctime) {
        System.out.println(ctime);
        if (ctime <= 0) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(ctime)).toString();
    }

    @SuppressWarnings("deprecation")
    public static String getDate(String date) {
        try {
            return getDate(Date.parse(date));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 
     * @param ctime
     * @return 返回完整的时�? 并且换行
     */
    public static String getDateBr(long ctime) {
        if (ctime <= 0) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String shortDate = format.format(new Date(ctime)).toString();

        return shortDate.replace(" ", "<br/>");
    }

    public static String getOrderNo() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(date);
    }

    /**
     * 获取现在时间当天的秒�?
     * 
     * 
     * @return返回
     */
    public static long getNowDateMs() {
        return DateUtils.String2Long(getNowDateShort(), "yyyy-MM-dd");
    }


    /**
     * 获取现在时间
     * 
     * @return返回字符串格�? yyyy-MM-dd HH:mm:ss
     */
    public static String getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     * 
     * @return返回字符串格�? yyyy年MM月dd日HH:mm
     */
    public static String getNowDate2() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     * 
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getNowDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     * 
     * @return 返回短时间字符串格式yyyy-MM
     */
    public static String getNowMonthShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 
     * 获取当前时间的第几天后的时间
     * 
     * @author zhuyp
     * @date 2015年8月10日 下午5:50:15
     * @param i
     * @return
     */
    public static String getNowDateByDays(int i) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.DATE, i);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    /**
     * 
     * 根据传入的秒数和 延迟的天数得到时间
     * 
     * @author zhuyp
     * @date 2015年8月10日 下午6:26:52
     * @param times
     * @param days
     * @return
     */
    public static Date GetDateByDaysSecond(int times, int days) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.DATE, days);
        calendar.add(Calendar.SECOND, times);
        return calendar.getTime();
    }

    /**
     * 
     * 根据传入的延迟秒数和 延迟的天数得到时间
     * 
     * @author zhuyp
     * @date 2015年8月10日 下午6:26:52
     * @param times
     * @param days
     * @return
     */
    public static Date GetDateByDaysTimes(String times, int days) {

        String timeString = getNowDateByDays(days) + times;
        return parseToDate(timeString, "yyyy-MM-ddHH:mm:ss");
    }

    /**
     * 
     * 根据传入的延迟秒数和 延迟的天数得到时间
     * 
     * @author zhuyp
     * @date 2015年8月10日 下午6:26:52
     * @param times
     * @param days
     * @return
     */
    public static Date GetDateByTimes(String times) {
        return parseToDate(times, "HH:mm:ss");
    }

    /**
     * 获取当前时间的第几个月后的时间
     * 
     * @param i
     * @return
     */
    public static String getNowDateCalcMonthShort(int i) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.MONTH, i);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    /**
     * 获取当前时间的往前一天再往前第几个月的时间
     * 
     * @param i
     * @return
     */
    public static String getYesDateCalcMonthShort(int i) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.add(Calendar.MONTH, i);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    /**
     * 得到当前时间的上个月 格式:yyyy-MM
     * 
     * @return
     */
    public static String getLastMonth() {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String dateString = sdf.format(calendar.getTime());
        return dateString;
    }

    /**
     * 得到当前时间的3个月 格式:yyyy-MM
     * 
     * @return
     */
    public static String getLastTreeMonth() {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.MONTH, -2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String dateString = sdf.format(calendar.getTime());
        return dateString;
    }

    /**
     * 返回上一个月,格式为:yyyy-mm
     * 
     * @param date
     * @return
     */
    public static String getLastMonthByDate(String date) {
        Date time = strToDate(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String dateString = sdf.format(calendar.getTime());
        return dateString;
    }

    /**
     * 返回i一个月,格式为:yyyy-mm
     * 
     * @param i
     * @return
     */
    public static String getNowDateMonth(int i) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.MONTH, i);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    /**
     * 获取星期一
     * 
     * @param date
     * @return
     */
    public static String getMonDay(int n) {
        Calendar cal = Calendar.getInstance();
        // n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
        String monday;
        cal.add(Calendar.DATE, n * 7);
        // 想周几，这里就传几Calendar.MONDAY（TUESDAY...）
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return monday;
    }

    /**
     * 获取星期日
     * 
     * @param date
     * @return
     */
    public static String getSunDay(int n) {
        Calendar cal = Calendar.getInstance();
        // n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
        String monday;
        cal.add(Calendar.DATE, n * 7);
        // 想周几，这里就传几Calendar.MONDAY（TUESDAY...）
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return monday;
    }

    /**
     * 获取当前时间的往前几天再往前第几个月的时间
     * 
     * @param i
     * @return
     */
    public static String getYesDateCalcMonthShort(int i, int j) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.DATE, j);
        calendar.add(Calendar.MONTH, i);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    public static String getNowDateCalcMonth(int i) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.MONTH, i);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    public static String getNowDateCalcDayShort(int i) {
        System.out.println(i + "========================");
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.DAY_OF_MONTH, i);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    /**
     * 获取时间的的最后时分秒链接字符串
     * 
     * @param sDate
     * @return
     */
    public static String getDateEnd(String sDate) {
        String str = sDate;
        str += " 23:59:59";
        return str;
    }

    /**
     * 获取时间的的开始的时分秒链接字符串
     * 
     * @param sDate
     * @return
     */
    public static String getDateStart(String sDate) {
        String str = sDate;
        str += " 00:00:00";
        return str;
    }

    /**
     * 
     * 获取当前时间 hhmmss
     * 
     * @author zhuyp
     * @date 2015年8月10日 下午9:07:45
     * @return
     */
    public static String getTimeShort() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date currentTime = new Date();
        String dateString = formatter.format(currentTime);
        return dateString;
    }


    /**
     * 
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     * 
     * @author zhuyp
     * @date 2015年8月10日 下午6:12:50
     * @param strDate
     * @return
     */
    public static Date parseToDate(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(strDate);
        } catch (Exception e) {
            return new Date();
        }
    }

    /**
     * 
     * 根据传入的格式 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     * 
     * @author zhuyp
     * @date 2015年8月10日 下午6:13:31
     * @param strDate
     * @param pattern
     * @return
     */
    public static Date parseToDate(String strDate, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(strDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
     * 
     * @param dateDate
     * @return
     */
    public static String dateToStrLong(java.util.Date dateDate) {
        if (dateDate == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     * 
     * @param dateDate
     * @param k
     * @return
     */
    public static String dateToStr(java.util.Date dateDate) {
        if (dateDate == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     * 
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public final static long String2Long(String sDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(sDate, pos).getTime();
    }

    public final static long String2Long(String sDate, String fomat) {
        if (sDate.length() == 19)
            fomat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat formatter = new SimpleDateFormat(fomat);
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(sDate, pos).getTime();
    }

    public final static long String2LongEnd(String sDate) {
        sDate += " 23:59:59";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(sDate, pos).getTime();
    }

    /**
     * 得到现在时间
     * 
     * @return
     */
    public static Date getNow() {
        Date currentTime = new Date();
        return currentTime;
    }

    /**
     * 提取�?个月中的�?后一�?
     * 
     * 
     * @param day
     * @return
     */
    public static Date getLastDate(long day) {
        Date date = new Date();
        long date_3_hm = date.getTime() - 3600000 * 34 * day;
        Date date_3_hm_date = new Date(date_3_hm);
        return date_3_hm_date;
    }

    /**
     * 得到现在时间
     * 
     * @return 字符�? yyyyMMdd HHmmss
     */
    public static String getStringToday() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 得到现在时间
     * 
     * @return yyyyMMdd
     */
    public static String getStringToday2() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }


    /**
     * 得到现在小时
     */
    public static String getHour() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String hour;
        hour = dateString.substring(11, 13);
        return hour;
    }

    /**
     * 得到现在分钟
     * 
     * @return
     */
    public static String getTime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String min;
        min = dateString.substring(14, 16);
        return min;
    }

    /**
     * 根据用户传入的时间表示格式，返回当前时间的格�? 如果是yyyyMMdd，注意字母y不能大写�?
     * 
     * 
     * @param sformat yyyyMMddhhmmss
     * @return
     */
    public static String getUserDate(String sformat) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(sformat);
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 得到计算后时�?
     * 
     * @param sformat yyyy-MM-dd HH:mm
     * @param field Calendar.xx
     * @param amount 偏移�?
     * @return
     */
    public static String getCalcDate(String sformat, int field, int amount) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(field, amount);
        SimpleDateFormat formatter = new SimpleDateFormat(sformat);
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }



    /**
     * 得到�?个时间延后或前移几天的时�?,nowdate为时�?,delay为前移或后延的天�?
     */
    public static String getNextDay(String nowdate, String delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String mdate = "";
            Date d = strToDate(nowdate);
            long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
            d.setTime(myTime * 1000);
            mdate = format.format(d);
            return mdate;
        } catch (Exception e) {
            return "";
        }
    }



    /**
     * 判断二个时间是否在同�?个周
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameWeekDates(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (0 == subYear) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
            // 如果12月的�?后一周横跨来年第�?周的话则�?后一周即算做来年的第�?�?

            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        }
        return false;
    }

    /**
     * 产生周序�?,即得到当前时间所在的年度是第几周
     * 
     * @return
     */
    public static String getSeqWeek() {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
        if (week.length() == 1)
            week = "0" + week;
        String year = Integer.toString(c.get(Calendar.YEAR));
        return year + week;
    }

    /**
     * 获得�?个日期所在的周的星期几的日期，如要找�?2002�?2�?3日所在周的星期一是几�?
     * 
     * 
     * @param sdate
     * @param num
     * @return
     */
    public static String getWeek(String sdate, String num) {
        // 再转换为时间
        Date dd = DateUtils.strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(dd);
        if (num.equals("1")) // 返回星期�?�?在的日期
            c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        else if (num.equals("2")) // 返回星期二所在的日期
            c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
        else if (num.equals("3")) // 返回星期三所在的日期
            c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
        else if (num.equals("4")) // 返回星期四所在的日期
            c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
        else if (num.equals("5")) // 返回星期五所在的日期
            c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
        else if (num.equals("6")) // 返回星期六所在的日期
            c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        else if (num.equals("0")) // 返回星期日所在的日期
            c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    }

    /**
     * 根据�?个日期，返回是星期几的字符串
     * 
     * @param sdate
     * @return
     */
    public static String getWeek(String sdate) {
        // 再转换为时间
        Date date = DateUtils.strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // int hour=c.get(Calendar.DAY_OF_WEEK);
        // hour中存的就是星期几了，其范�? 1~7
        // 1=星期�? 7=星期六，其他类推
        return new SimpleDateFormat("EEEE").format(c.getTime());
    }

    public static String getWeekStr(String sdate) {
        String str = "";
        str = DateUtils.getWeek(sdate);
        if ("1".equals(str)) {
            str = "星期�?";
        } else if ("2".equals(str)) {
            str = "星期�?";
        } else if ("3".equals(str)) {
            str = "星期�?";
        } else if ("4".equals(str)) {
            str = "星期�?";
        } else if ("5".equals(str)) {
            str = "星期�?";
        } else if ("6".equals(str)) {
            str = "星期�?";
        } else if ("7".equals(str)) {
            str = "星期�?";
        }
        return str;
    }

    /**
     * 两个时间之间的天�?
     * 
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static long getDays(String date1, String date2) {
        if (date1 == null || date1.equals(""))
            return 0;
        if (date2 == null || date2.equals(""))
            return 0;
        // 转换为标准时�?

        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = null;
        java.util.Date mydate = null;
        try {
            date = myFormatter.parse(date1);
            mydate = myFormatter.parse(date2);
        } catch (Exception e) {
        }
        long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
        return day;
    }

    /**
     * 
     * 判断传入的时间是否比当前时间大
     * 
     * @author zhuyp
     * @date 2015年8月10日 下午9:17:17
     * @param date1
     * @param date2
     * @return
     */
    public static Boolean IsOld(String date1) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("HH:mm:ss");
        Date date = null;
        Date mydate = null;
        try {
            mydate = myFormatter.parse(date1);
            date = myFormatter.parse(getTimeShort());
        } catch (Exception e) {
        }
        long day = mydate.getTime() - date.getTime();
        if (day < 0) {
            return true;
        }
        return false;
    }

    /**
     * 两个日期date1-date2相减，相差的天数(包含首尾天)
     * 
     * @param date1 日期
     * @param date2 日期
     * @return 返回相减后的日期
     */
    public static int getDateDiff(Date date1, Date date2) {
        long nowDate = getMillis(date1);
        long afterDate = getMillis(date2);
        if (nowDate == afterDate) {
            return 1;
        } else {
            return (int) ((nowDate - afterDate) / (24 * 3600 * 1000)) + 1;
        }
    }

    /**
     * 返回日期代表的毫秒
     * 
     * @param date 日期
     * @return 返回毫秒
     */
    public static long getMillis(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    public static Date getDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        String dateString = formatter.format(date);
        return formatter.parse(dateString, pos);
    }


}
